'''
Author: lololo 2246223018@qq.com
Date: 2024-01-05 15:48:20
LastEditors: lololo 2246223018@qq.com
LastEditTime: 2024-01-05 17:06:47
FilePath: /python/novelRolesMap/demo3.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import codecs

person = {} # 人名字典
relationships = {} # 关系字典
with open('53643.txt', 'r') as f:
    Lines = f.readlines()
with open('name_dict.txt', 'r') as f:
    Names = f.readlines()

for i in range(0,len(Names)):
    Names[i] = Names[i].replace('\n','')
    relationships[Names[i]] = {}
    person[Names[i]] = {}

# 建立关系
for line in Lines:
    for key1 in Names:
        if key1 in line:
            for key2 in Names:
                # 人物节点
                if key1 == key2:
                    if person[key1].get(key2) is None:
                        person[key1][key2] = 1
                    else:
                        person[key1][key2] += 1
                # 人物关系
                if key2 in line and key1 != key2:
                    if relationships[key1].get(key2) is None:
                        relationships[key1][key2] = 1
                    else:
                        relationships[key1][key2] += 1

print(person)
print(relationships)

# 保存到csv
with codecs.open('name_dict.csv', 'a+', 'utf-8') as f:
    f.write('Id,Label,Weight\r\n')
    for name,times in person.items():
        f.write(name+','+name+','+str(times)+'\r\n')

with codecs.open('name_ship.csv', 'a+', 'utf-8') as f:
    f.write('Source,Target,Weight\r\n')
    for name,edges in relationships.items():
        for v,w in edges.items():
            if w>2:
                f.write(name+','+v+','+str(w)+'\r\n')

# 然后绘制人物关系图
                